﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using Koonsoft.HYCRM.BLL;
using Wuqi.Webdiyer;

public partial class Company_CompanyList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // 默认不显示任何数据
        //if (!IsPostBack)
        //{
        //    BindGrid();
        //}
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        var pager = (AspNetPager) sender;
        var recordCount = 0;
        var pageIndex=0;
        try
        {
            pageIndex = Convert.ToInt32(Request["page"]) - 1;
            if (pageIndex < 0)
                pageIndex = 0;
        }
        catch (Exception)
        {
            pageIndex = 0;

        }
        
        var pageSize = Convert.ToInt32(ConfigurationManager.AppSettings["commonPageSize"]);

        var orderStart = OrderStart.Text.Trim();
        var orderEnd = OrderEnd.Text.Trim();
        var englishName = EnglishName.Text.Trim();
        var chineseName = ChineseName.Text.Trim();
        var companyID = CompanyID.Text.Trim();
        var regionID = Convert.ToInt32(DropDownList1.SelectedValue);

        AspNetPager1.PageSize = pageSize;

        GridView1.DataSource = new CompanyBLL().GetCompaniesByPager(orderStart, orderEnd, englishName, chineseName, companyID, regionID, pageSize, pageIndex, out recordCount);
        GridView1.DataBind();

        AspNetPager1.RecordCount = recordCount;
        this.AspNetPager1.CurrentPageIndex = pageIndex + 1;
    }

    private void BindGrid()
    {
        var recordCount = 0;
        var pageIndex = 0;
        var pageSize = Convert.ToInt32(ConfigurationManager.AppSettings["commonPageSize"]);

        var orderStart = OrderStart.Text.Trim();
        var orderEnd = OrderEnd.Text.Trim();
        var englishName = EnglishName.Text.Trim();
        var chineseName = ChineseName.Text.Trim();
        var companyID = CompanyID.Text.Trim();
        var regionID = Convert.ToInt32(DropDownList1.SelectedValue);

        AspNetPager1.PageSize = pageSize;

        GridView1.DataSource = new CompanyBLL().GetCompaniesByPager(orderStart, orderEnd, englishName, chineseName, companyID, regionID, pageSize, pageIndex, out recordCount);
        GridView1.DataBind();

        AspNetPager1.RecordCount = recordCount;
        this.AspNetPager1.CurrentPageIndex = 1;
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "RemoveCompany")
        {
            var serialNumber = e.CommandArgument.ToString();

            new CompanyBLL().Remove(serialNumber);

            BindGrid();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        BindGrid();
    }
}